package com.koron.sinoprobe.web.engineering.service.impl;


import com.koron.sinoprobe.web.engineering.bean.po.SpWarrantyConstruction;
import com.koron.sinoprobe.web.engineering.mapper.SpWarrantyConstructionMapper;
import com.koron.sinoprobe.web.engineering.service.SpWarrantyConstructionService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 质保期满验收-施工单位(SpWarrantyConstruction)表服务实现类
 *
 * @author jzp
 * @since 2022-03-03 09:22:00
 */
@Service
public class SpWarrantyConstructionServiceImpl implements SpWarrantyConstructionService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpWarrantyConstruction getById(SessionFactory factory, String id) {
        SpWarrantyConstructionMapper mapper = factory.getMapper(SpWarrantyConstructionMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpWarrantyConstruction> list(SessionFactory factory, SpWarrantyConstruction spWarrantyConstruction) {
        SpWarrantyConstructionMapper mapper = factory.getMapper(SpWarrantyConstructionMapper.class);
        return mapper.selectList(spWarrantyConstruction);
    }

    @Override
    @TaskAnnotation("listByWarrantyId")
    public List<SpWarrantyConstruction> listByWarrantyId(SessionFactory factory, String warrantyId) {
        SpWarrantyConstruction spWarrantyConstruction = new SpWarrantyConstruction();
        spWarrantyConstruction.setWarrantyId(warrantyId);
        return list(factory, spWarrantyConstruction);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, SpWarrantyConstruction spWarrantyConstruction) {
        SpWarrantyConstructionMapper mapper = factory.getMapper(SpWarrantyConstructionMapper.class);
        return mapper.insertOrUpdate(spWarrantyConstruction) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<SpWarrantyConstruction> lists) {
        if (CollectionUtils.isEmpty(lists)) {
            return false;
        }
        SpWarrantyConstructionMapper mapper = factory.getMapper(SpWarrantyConstructionMapper.class);
        return mapper.insertOrUpdateBatch(lists) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpWarrantyConstruction spWarrantyConstruction) {
        SpWarrantyConstructionMapper mapper = factory.getMapper(SpWarrantyConstructionMapper.class);
        return mapper.update(spWarrantyConstruction) > 0;
    }


    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        SpWarrantyConstructionMapper mapper = factory.getMapper(SpWarrantyConstructionMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByWarrantyId")
    public boolean removeByWarrantyId(SessionFactory factory, String warrantyId) {
        if (StringUtils.isEmpty(warrantyId)) {
            return false;
        }
        List<SpWarrantyConstruction> constructionList = listByWarrantyId(factory, warrantyId);
        return deleteBatchIds(factory, constructionList.stream().map(SpWarrantyConstruction::getWarrantyId).collect(Collectors.toList()));

    }
}
